package rabbit.topic;

import com.rabbitmq.client.*;
import com.rabbitmq.client.impl.AMQImpl;
import com.rabbitmq.client.impl.Method;
import com.rabbitmq.client.impl.MethodArgumentWriter;

import java.io.IOException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/7/31 14:56
 */


public class TestRpcServer {
    private static final String EXCHANGE_NAME ="testrpcexchange";// "outerhostexchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        //factory.setHost("39.106.17.245");
        //factory.setHost("39.106.144.161");
        factory.setHost("192.168.10.66");//"03156721261"
        factory.setVirtualHost("OuterHost");
        factory.setUsername("root");
        factory.setPassword("root");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true);
channel.queueDeclare("testrpcqueue",false,false,false,null);
channel.queuePurge("testrpcqueue");
        channel.basicQos(1);


        channel.basicConsume("testrpcqueue", false, new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {

                AMQP.BasicProperties replyProps=new AMQP.BasicProperties.
                        Builder().correlationId(delivery.getProperties().
                        getCorrelationId()).build();
                System.out.println(delivery.getProperties().getReplyTo());
                System.out.println(new String(delivery.getBody(),"UTF-8"));
                channel.basicPublish("",delivery.getProperties().getReplyTo(),
                        replyProps,"123".getBytes("UTF-8"));
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
            }
        }, new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        });
    }
}